// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SqlAzureDatabaseInfo.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace ExecutionTool.CopyDatabase
{
    using System;

    public class SqlAzureDatabaseInfo
    {
        #region Constructors and Destructors

        public SqlAzureDatabaseInfo(SqlAzureDatabaseEdtion edition, long maxSizeInBytes)
        {
            var maxSizeInGigaBytes = (int)(maxSizeInBytes / (1024 * 1024 * 1024));
            this.ValidateMaxSize(edition, maxSizeInGigaBytes, "maxSizeInBytes");

            this.Edition = edition;
            this.MaxSizeInGb = maxSizeInGigaBytes;
        }

        public SqlAzureDatabaseInfo(string editionText, long maxSizeInBytes, double actualSizeInMb)
        {
            this.ActualSizeInMb = actualSizeInMb;
            SqlAzureDatabaseEdtion edition;

            if (!Enum.TryParse(editionText, true, out edition))
            {
                throw new ArgumentException("Edition text can be only Web or Business.", "editionText");
            }

            var maxSizeInGigaBytes = (int)(maxSizeInBytes / (1024 * 1024 * 1024));
            this.ValidateMaxSize(edition, maxSizeInGigaBytes, "maxSizeInBytes");

            this.Edition = edition;
            this.MaxSizeInGb = maxSizeInGigaBytes;
        }

        public SqlAzureDatabaseInfo(string editionText, string maxSizeInGigaBytesText)
        {
            SqlAzureDatabaseEdtion edition;

            if (!Enum.TryParse(editionText, true, out edition))
            {
                throw new ArgumentException("Edition text can be only Web or Business.", "editionText");
            }

            int maxSizeInGigaBytes;

            if (!int.TryParse(maxSizeInGigaBytesText.Substring(0, maxSizeInGigaBytesText.Length - 2), out maxSizeInGigaBytes))
            {
                throw new ArgumentException("MaxSizeInGb text can be only in format ##gb.", "maxSizeInGigaBytesText");
            }

            this.ValidateMaxSize(edition, maxSizeInGigaBytes, "maxSizeText");

            this.Edition = edition;
            this.MaxSizeInGb = maxSizeInGigaBytes;
        }

        #endregion

        #region Public Properties

        public double ActualSizeInMb { get; private set; }

        public SqlAzureDatabaseEdtion Edition { get; private set; }

        public long MaxSizeInGb { get; private set; }

        #endregion

        #region Methods

        private void ValidateMaxSize(SqlAzureDatabaseEdtion edition, int maxSizeInGigaBytes, string paramName)
        {
            if (edition == SqlAzureDatabaseEdtion.Web)
            {
                switch (maxSizeInGigaBytes)
                {
                    case 1:
                    case 5:
                        break;
                    default:
                        throw new ArgumentException("Not allowed max size for Web edition.", paramName);
                }
            }
            else if (edition == SqlAzureDatabaseEdtion.Business)
            {
                switch (maxSizeInGigaBytes)
                {
                    case 10:
                    case 20:
                    case 30:
                    case 40:
                    case 50:
                    case 100:
                    case 150:
                        break;
                    default:
                        throw new ArgumentException("Not allowed max size for Business edition.", paramName);
                }
            }
            else
            {
                throw new ArgumentException("Only Web or Business edition is allowed.");
            }
        }

        #endregion
    }
}
